const app = getApp()
var timeCount  = null
import { HiddenCall } from '../../request/api'
const request = new HiddenCall
import tab_index from '../../mixins/tab_index'
import filter from '../../mixins/filter.js'
import { throttle } from '../../utils/throttle.js'
// 跟进数据组织跳转
import { describeData } from '../../utils/method.js'
Page({
  /**
   * 消列表
   * unRead             未读的消息
   */
  mixins: [tab_index, filter],
  data: {
    unRead: app.globalData.unread,
    showSkeleton: true,
    scrollTop: 0,
    flag: 0,
    animation: false
  },
  onLoad: function () {
    let that = this
    wx.getStorage({
      key: "showSkeleton",
      success: function (res) {
        that.setData({
          showSkeleton: false
        })
      },
      fail: function () {
        // 骨架屏关闭渲染
        setTimeout(() => {
          that.setData({ showSkeleton: false })
        }, 2000)
        wx.setStorage({
          key: "showSkeleton",
          data: "true"
        })
      }
    })
    this.resourceCode = ""
    // 获取屏幕的高度
    this.getHeight("index")
    // this.refreshView = this.selectComponent("#refreshView")
    wx.showNavigationBarLoading()
    // 验证是否有跟进未写
    request.followFinished().then((res) => {
      // 跳转到跟进页面
      if (res.result) describeData('index', res, true)
    })
  },
  onReady() {
    let that = this
    this.setData({ page: 1 })
    // 调用消息列表
    this.messageList({ "page": 1, "size": this.data.size })
  },
  /**
   * 消息列表调用
   */
  messageList: function (model) {
    let that = this
    timeCount = setInterval(() => {
      this.data.flag += 1
      if (this.data.flag > 0) {
        that.setData({ animation: true })
      }
    }, 200)
    // 传递参数调用接口
    request.messageList(model).then((res) => {
      if (res) {
        this.setData({
          messageList: JSON.parse(JSON.stringify(res)),
          list: res,
          unRead: res.unread,
          count: res.page
        })
        wx.stopPullDownRefresh()
        wx.hideNavigationBarLoading()
        // 停止计算
        clearInterval(timeCount)
        timeCount = null
        that.setData({ animation: false })
      }
      // 拿到未读
      app.globalData.unread = this.data.unRead
      // 渲染高亮相关字符
      this.textHighlight(this.data.list.content)
      this.getListHeight()
    }).catch((err) => {
      // 获取失败信息
      this.setData({ errCode: err })
    })
  },
  /**
   * 点击搜索符合搜索条件的消息数据
   * author wl
   */
  searchMessage(e) {
    // 房源编号
    this.resourceCode = e.detail
    // 初始页数
    this.data.page = 1
    // 调用接口
    this.messageList({ "page": this.data.page, "size": this.data.size, resourceCode: e.detail })
  },
  tiggerSms () {
    this.messageList({ "page": 1, "size": this.data.size })
  },
  /**
   * 下拉刷新消息列表数据
   */
  onPullDownRefresh() {
    // 有搜索条件调用, isShowTab: false 
    this.setData({ page: 1})
    if (this.resourceCode !== "") {
      this.messageList({ "page": 1, "size": this.data.size, resourceCode: this.resourceCode })
    } else {
      // 无搜索条件调用
      this.messageList({ "page": 1, "size": this.data.size })
    }
    wx.showNavigationBarLoading()
  },
  /**
   * 消息列表上拉加载操作
   */
  onReachBottom: function() {
    // 加载的页数
    wx.showNavigationBarLoading()
    this.data.page += parseInt(this.data.count)
    // 有筛选条件，带条件调用
    if (this.resourceCode !== "") {
      let data = {
        "page": this.data.page,
        "size": this.data.size,
        "resourceCode": this.resourceCode
      }
      this.pullMessage(data)
    } else {
      // 无条件，普通调用
      this.pullMessage({ "page": this.data.page, "size": this.data.size })
    }
  },
  /**
   * 消息列表上拉加载数据
   */
  pullMessage: function(model) {
    // 数据全部加载完数据之后，禁止调用接口
    if (this.data.list.content.length === this.data.list.count) {
      wx.hideNavigationBarLoading()
      return
    }
    request.messageList(model, 0).then(res => {
      // 拿到数据循环放入渲染的model
      res.content.forEach(res => {
        this.data.messageList.content.push(JSON.parse(JSON.stringify(res)))
        this.data.list.content.push(res)
        this.setData({ 
          list: this.data.list,
          messageList: this.data.messageList
        })
      })
      wx.hideNavigationBarLoading()
      // 高亮新的数据
      this.textHighlight(res.content)
      this.getListHeight()
    })
  },
  /**
   * 消息列表文字高亮
   * author wl
   */
  textHighlight(list) {
    if (list) {
      for (let i = 0; i < list.length; i++) {
        list[i].checked = false
        // 高亮数字
        this.numberHighlight(list[i])
        // 高亮编号
        this.codeHighlight(list[i])
        //高亮分享名字
        this.nameHighlight(list[i])
      }
      this.setData({ list: this.data.list })
    }
  },
  /**
   * 将推送消息条数高亮
   * author wl
   */
  numberHighlight(text) {
    // 查找关于条的字符
    let str = text.content.match(/\w+条/)
    if (str !== null) {
      // 放入模板渲染
      let txt = `<span style='color:red;padding: 0 4px;'>${str[0][0]}</span>`
      text.content = text.content.replace(/(\d{1})/, txt)
    }
  },
  /**
   * 将推送消息编号高亮如：ERP新推送：客源sh188
   * author wl
   */
  codeHighlight(text) {
    let content = text
    if (text.content.split(/源+/y)[1]) {
      // 区分房客源
      let title = content.utype === 'house' ? '房源' : '客源'
      let code = text.content.split(/源+/y)[1].split(/已+/y)[0]
      let contain = title + code
      // 高亮房客源+房客编号
      let lodger = `<span style='font-weight: bold;padding: 0  4px 0 0;'>${title}</span><span style='font-weight: bold;padding: 0  4px 0 0;'>${code}</span>`
      text.content = text.content.replace(contain, lodger)
    }
  },
  /**
   * 将分享名字高亮如：小张向你分享了
   * author wl
   */
  nameHighlight(text) {
    // 查找关键字
    let name = text.content.match(/(\S*)向/)
    if (name !== null) {
      // 模板渲染
      let replaceName = `<span style='font-weight: bold;padding: 0  3px 0 0;'>${name[1]}</span>向`
      text.content = text.content.replace(/[\u4e00-\u9fa5](\S*)向/, replaceName)
    }
  },
  /**
   * 获取列表的高度
   */
  getListHeight () {
    let that = this
    setTimeout(() => {
      // 获取当前的查询
      wx.createSelectorQuery().select('.list-message').boundingClientRect((res) => {
        that.setData({
          listHeight: res ? res.height : 0
        })
      }).exec()
    }, 500)
  }
})
